package unitTest;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.en.PorterStemFilter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class lucene_stop_stramming {
    // 只是写在了contructor外面了， 实际在constructor里面执行
    BufferedReader reader = new BufferedReader(new FileReader("english.txt"));
    private Analyzer analyzer = new StandardAnalyzer();

    public lucene_stop_stramming() throws IOException {

    }

    @Test
    public void testStopWords() throws IOException {
        String stopstr = "The donkey in a green tree, it eat grass happily";
        StringReader reader = new StringReader(stopstr);
        TokenStream ts = analyzer.tokenStream("", reader);
//        ts = new PorterStemFilter(ts);
        CharTermAttribute charTermAttribute = ts.addAttribute(CharTermAttribute.class);
        ts.reset();

        List<String> list = new ArrayList<>();
        while(ts.incrementToken()){
            String term = charTermAttribute.toString();
            list.add(term);
        }
        System.out.println(stopstr);
        System.out.println(list);
    }
}
